package com.qianzhou.mapper;

import com.qianzhou.pojo.Order;
import com.qianzhou.pojo.Role;
import com.qianzhou.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 查询当前用户信息 resultType
     *
     * @return List<User>
     */
    List<User> findAll();

    /**
     * 添加用户信息
     *
     * @param user
     */
    void addUser(User user);

    /**
     * 更新用户信息
     *
     * @param user
     */
    void updateUser(User user);

    /**
     * 删除用户信息
     *
     * @param id
     */
    void deleteUser(Integer id);

    /**
     * 根据ResultMap的方式查询所有用户信息
     *
     * @return List<User>
     */
    List<User> findAll2();

    /**
     * 保存用户信息，返回对应的主键的信息
     *
     * @param user
     */
    public void save(User user);

    /**
     * 根据条件查询用户信息
     *
     * @param user
     * @return
     */
    public List<User> findByIdAndUsernameIf(User user);

    /**
     * 动态更新user表数据，如果该属性有值就更新，没有值不做处理
     *
     * @param user
     */
    public void updateIf(User user);

    /**
     * 根据用户列表的id查询用户信息
     *
     * @param ids
     * @return
     */
    public List<User> findByList(List<Integer> ids);

    /**
     * 根据用户查询对应的订单信息
     */
    public List<User> findAllWithOrder();

    /**
     * 查询用户对应的角色信息
     *
     * @return
     */
    public List<User> findAllWithRole();

    /**
     * 用注解的方式查找用户信息
     *
     * @return
     */
    @Select("SELECT * FROM `user`")
    public List<User> findAllAnnotation();

    /**
     * 用注解的方式保存用户信息
     */
    @Insert("INSERT INTO `user`(username,password,birthday) VALUES(" +
            "#{username},#{password},#{birthday})")
    public void saveAnnotation(User user);

    /**
     * 用注解的方法修改用户信息
     */
    @Update("UPDATE `user` SET username = #{username},birthday = #{birthday},password = #{password} WHERE id = #{id}")
    public void updateAnnotation(User user);

    /**
     * 根据id删除用户信息
     */
    @Delete("DELETE FROM `user` where id = #{id}")
    public void delete(Integer id);

    /**
     * 使用注解的方法实现一对一和多对一
     */
    @Select("select * from orders")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "ordertime", property = "ordertime"),
            @Result(column = "total", property = "total"),
            @Result(property = "user", javaType = User.class,
                    column = "uid", one = @One(select = "com.qianzhou.mapper.UserMapper.findById", fetchType = FetchType.EAGER))
    })
    public List<Order> findAllWithUserAnnotation();

    @Select("SELECT * FROM `user` WHERE id = #{id}")
    public User findById();

    /**
     * 使用注解的方法实现一对多方式
     * @return
     */
    @Select("select * from `user`")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(property = "orderList", javaType = List.class, column = "id",
            many = @Many(select = "com.qianzhou.mapper.OrderMapper.findByUid"))
    })
    public List<User> findAllWithOrderAnnotation();

    /**
     * 使用注解的方式实现多对多的方式
     */
    @Select("select * from `user`")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(property = "roleList",javaType = List.class, column = "id",
            many = @Many(select = "com.qianzhou.mapper.UserMapper.findByUid"))
    })
    public List<User> findAllWithRoleAnnotation();
    @Select("select * from sys_role r inner join sys_user_role ur ON r.`id` = ur.`roleid`" +
            "WHERE ur.`userid` = #{uid}")
    public List<Role> findByUid(Integer uid);
}
